<template>
  <div class="login-container">
    <div class="login-form">
      <h1>登录</h1>
      <el-form ref="ruleFormRef" :model="loginForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="submitForm(ruleFormRef)">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { ElForm, ElFormItem, ElInput, ElButton, ElMessage,ComponentSize, FormInstance, FormRules  } from 'element-plus';
import { useRouter } from 'vue-router';
import { useAuthStore } from '~/stores/index';
// import { useUserStore } from '~/stores/user';
import CryptoJS from 'crypto-js';
import { getPasswordLogin } from "~/utils/api/login"
import { useHttp } from '~/composables/useHttp';
const router = useRouter();
const authStore = useAuthStore();
interface RuleForm {
  username: string
  password: string

}
const loading = ref(false);
const fetchUsers = async () => {
  loading.value = true;
  const lognParams = {
    account: loginForm.value.username,
    password: CryptoJS.MD5(loginForm.value.password).toString(),
  }
  const res =  await getPasswordLogin(lognParams);
  console.log("login",res)
  const data = res.data.value;
  if(res.data.value.code == 200){
    router.push('/');
    // useUserStore.loggedIn = true;
    // useUserStore.userinfo = res.data.data;
    authStore.loggedIn = true;
    authStore.userinfo = res.data.data;
    console.log("用户信息",useUserStore)

    ElMessage({
      message: '登录成功',
      type: 'success',
    })
  }else {
    ElMessage({
      message:res.data.value.info,
      type: 'error',
    })

  }
  loading.value = false;
};
// fetchUsers();
const ruleFormRef = ref<FormInstance>()
const loginForm = ref<RuleForm>({
  username: '',
  password: ''
});




const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {

      fetchUsers();


    } else {
      console.log('error submit!', fields)
    }
  })
}

</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-form {
  width: 500px;
  padding: 20px;
  background-color: #ffffff;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  h1{
    text-align: center;
  }
}

.demo-ruleForm {
  margin-top: 20px;
}
</style>